package com.bps.zzant.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.bps.zzant.model.Customer;
import com.bps.zzant.model.Product;

public class ProductDAOImpl implements ProductDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	public List getProductByStyleNo(String styleNo) {
		System.out.print("----++++++-----ProductDAOImpl.getProductByStyleNo:" + styleNo);
		List<Product> productList = sessionFactory.getCurrentSession().createCriteria(Product.class).add(Restrictions.like("styleNo", styleNo)).list();
		return productList;
	}

	public List<Product> getAllProducts() {
		System.out.print("---------ProductDAOImpl.getAllProducts:" );
		List<Product> productList = sessionFactory.getCurrentSession().createCriteria(Product.class).add(Restrictions.like("styleNo", "%")).list();
		return productList;
	}
	
	public List getProductByDescription(String desc) {
		System.out.print("----++++++-----ProductDAOImpl.getProductByDescription:" + desc);
		List<Product> productList = sessionFactory.getCurrentSession().createCriteria(Product.class).add(Restrictions.like("description", "%"+desc+"%")).list();
		return productList;
	}
	
	public void saveProduct(Product product){
		System.out.println("-------before Product is saved. The product id: " + product.getProdId());
		sessionFactory.getCurrentSession().save(product);
		sessionFactory.getCurrentSession().flush();
		System.out.println("-------after roduct is saved. The product id: " + product.getProdId());		
	}
	
	public void updateProduct(Product product) {
		sessionFactory.getCurrentSession().merge(product);
		sessionFactory.getCurrentSession().flush();		
	}
}
